package org.budo.dubbo;

import java.lang.reflect.Constructor;

import org.budo.support.lang.util.ReflectUtil;
import org.budo.support.lang.util.StringUtil;

import com.alibaba.dubbo.rpc.RpcContext;

import lombok.extern.slf4j.Slf4j;

/**
 * @author lmw
 */
@Slf4j
public class Dubbo {
    public static Object applicationConfig(String name) {
        Class type = ReflectUtil.classForName("com.alibaba.dubbo.config.ApplicationConfig");
        Constructor constructor = ReflectUtil.getConstructor(type, new Class[] { String.class });
        return ReflectUtil.newInstance(constructor, new String[] { name });
    }

    public static Boolean hasAttachment(String key) {
        if (StringUtil.isNullOrEmpty(key)) {
            return false;
        }

        RpcContext context = RpcContext.getContext();
        if (null == context) {
            return false;
        }

        String attachment = context.getAttachment(key);
        return StringUtil.isNotNullOrEmpty(attachment);
    }

    public static String failedInvokeMethodMessage(Exception exception) {
        String error = exception + "";

        return typeSimpleName_0(error) + "." + methodName_0(error);
    }

    private static String typeSimpleName_0(String error) {
        String str = "in the service";
        int a = error.indexOf(str);
        if (a < 0) {
            log.error("#39 no Failed to invoke the method, error=" + error);
            return "#40 typeSimpleName_0";
        }

        int b = error.indexOf(". Tried", a + str.length());
        if (b < 0) {
            log.error("#45 error=" + error);
            return "#40 typeSimpleName_0";
        }

        String type = error.substring(a, b);
        return ReflectUtil.simpleName(type);
    }

    private static String methodName_0(String error) {
        String str = "Failed to invoke the method ";
        int a = error.indexOf(str);
        if (a < 0) {
            log.error("#31 no Failed to invoke the method, error=" + error);
            return "#31 methodName";
        }

        int b = error.indexOf(" in the service", a + str.length());
        if (b < 0) {
            log.error("#57 error=" + error);
            return "#58 methodName";
        }

        return error.substring(a + str.length(), b);
    }

    public static String noProviderAvailableMessage(Exception exception) {
        String error = exception + "";

        String str = "No provider available ";
        int a = error.indexOf(str);
        if (a < 0) {
            log.error("#31 no noProviderAvailableMessage, e=" + exception, exception);
            return "#50 ######";
        }

        String str_c = " for service ";
        int c = error.indexOf(str_c);
        if (c < 0) {
            log.error("#55 c=" + c + ", error=" + error);
            return "#55 #####";
        }

        int d = error.indexOf(" on consumer ", c + str_c.length());
        if (d < 0) {
            log.error("#61 d=" + d + ", error=" + error);
            return "#60 ######";
        }

        String type = error.substring(c + str_c.length(), d);
        return "No provider for " + ReflectUtil.simpleName(type);
    }
}